﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;
using AutoMapper;
using Mes.Pendant.ArrivalNotices.Dto;
using Mes.Pendant.Common.DtoMapping;
using Mes.Pendant.MaterialTypes;
using Mes.Pendant.MaterialTypeService.Dto;
using Mes.Pendant.MaterialTypeService.Services;
using Mes.Pendant.NotificationArrivals;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Mes.Pendant.MaterialTypeServicess
{
	public class MaterialTypeServices : ApplicationService, IMaterialTypeServices
	{
		private readonly IRepository<MaterialType, int> materialTypeRepository;
		private readonly IMapper mapper;
		public MaterialTypeServices(IRepository<MaterialType, int> materialTypeRepository, IMapper mapper)
		{
			this.materialTypeRepository = materialTypeRepository;
			this.mapper = mapper;
		}
		/// <summary>
		/// 新增物料类型
		/// </summary>
		/// <param name="insertMaterialTypedto"></param>
		/// <returns></returns>
		public async Task<int> AddMaterialtype(InsertMaterialTypedto insertMaterialTypedto)
		{
			var materialtype= insertMaterialTypedto.MapTo<MaterialType>();
			await materialTypeRepository.InsertAsync(materialtype);
			return 1;
		}

		/// <summary>
		/// 获取物料产品树
		/// </summary>
		/// <returns></returns>
		public List<MaterialtypeTreedto> GetMaterialtypeTreedtoList(int pId)
		{
			var materialtypeList = materialTypeRepository.GetListAsync().Result;
			var trees = materialtypeList.Where(m => m.OrderBy == pId).Select(x => new MaterialtypeTreedto
			{
				Id = x.Id,
				Title = x.MaterialTypeName,
				Children = GetChildrenMaterialtypeList(materialtypeList, x.Id)
			}).ToList();
			return trees;
		}

		/// <summary>
		/// 获取物料产品树
		/// </summary>
		/// <returns></returns>
		public List<MaterialtypeTreedto> GetChildrenMaterialtypeList(List<MaterialType> materialtypeList, int pId)
		{
			var list = materialtypeList.Where(x => x.OrderBy == pId).ToList();
			var trees = list.Select(x => new MaterialtypeTreedto
			{
				Id = x.Id,
				Title = x.MaterialTypeName,
				Children = GetChildrenMaterialtypeList(materialtypeList, x.Id)
			}).ToList();
			return trees;
		}

		/// <summary>
		/// 根据ID获取物料型号
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public MaterialtypeTreedto GetMaterialtypeById(int id)
		{
			var list = materialTypeRepository.GetAsync(id).Result;
			return ObjectMapper.Map<MaterialType, MaterialtypeTreedto>(list);
		}

		/// <summary>
		/// 点击树形节点获取节点id和子集id
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<string> GetNumarr(int id)
		{
			string a = string.Empty;// "";
			var list = await materialTypeRepository.GetListAsync();
			a += Convert.ToString(id) + "," + await Getdoublearr(list, id);
			return a;
		}

		/// <summary>
		/// 获取树形节点子集(递归方法)
		/// </summary>
		/// <param name="list"></param>
		/// <param name="orderby"></param>
		/// <returns></returns>
		public async Task<string> Getdoublearr(List<MaterialType> list, int orderby)
		{
			string a = string.Empty;// "";
			var result = list.Where(x => x.OrderBy == orderby).ToList();
			if (result.Count == 0)
			{
				return a;
			}
			foreach (var item in result)
			{
				int id = item.Id;
				a += Convert.ToString(id) + "," + await Getdoublearr(list, id);
			}
			return a;
		}

		/// <summary>
		/// 物料产品分类显示
		/// </summary>
		/// <param name="page">当前页</param>
		/// <param name="limit">每页显示的条数</param>
		/// <param name="materialTypeName">分类名称</param>
		/// <returns>物料产品分类显示列表</returns>
		public async Task<PagedResult> GetMaterialTypeList(int page, int limit, string? materialTypeName)
		{
			var predicate = PredicateBuilder.New<MaterialType>(true);
			if (!string.IsNullOrEmpty(materialTypeName))
			{
				predicate = predicate.And(p => p.MaterialTypeName.Contains(materialTypeName));
			}

			var materialTypeList = materialTypeRepository.GetListAsync(predicate).Result;

			PagedResult responseResult = new PagedResult();
			responseResult.CurrentPage = page;
			responseResult.PageSize = limit;
			responseResult.RowCount = materialTypeList.Count();
			responseResult.PageCount = (int)Math.Ceiling((responseResult.RowCount) / (double)limit);
			responseResult.Queryable = materialTypeList.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).AsQueryable();
			responseResult.Queryable = responseResult.Queryable.MapToList<MaterialTypeListDto>().AsQueryable();
			return responseResult;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public async Task<List<MaterialTypeListDto>> GetMaterialTypeTreeTable(string? materialTypeName)
		{
			var predicate = PredicateBuilder.New<MaterialType>(true);
			if (!string.IsNullOrEmpty(materialTypeName))
			{
				predicate = predicate.And(m => m.MaterialTypeName.Contains(materialTypeName));
			}

			var materialTypeTreeTable = await materialTypeRepository.GetListAsync(predicate);
			var result = materialTypeTreeTable.MapToList<MaterialTypeListDto>();
			return result;
		}


		public async Task<int> AddMaterialType(AddMaterialTypeDto addMaterialTypeDto)
		{
			MaterialType materialTypeModel = addMaterialTypeDto.MapTo<MaterialType>();
			await materialTypeRepository.InsertAsync(materialTypeModel);
			return materialTypeModel.Id;
		}

		public async Task<int> RemoveMaterialTypeById(int id)
		{
			await materialTypeRepository.DeleteAsync(id);
			return 1;
		}

		public async Task<EditMaterialTypeDto> FindMaterialType(int id)
		{
			MaterialType MaterialTypeModel = await materialTypeRepository.GetAsync(id);
			EditMaterialTypeDto editMaterialTypeDto = MaterialTypeModel.MapTo<EditMaterialTypeDto>();
			return editMaterialTypeDto;
		}

		public async Task<int> UpdateMaterialType(EditMaterialTypeDto editMaterialTypeDto)
		{
			MaterialType materialTypeModel = editMaterialTypeDto.MapTo<MaterialType>();
			await materialTypeRepository.UpdateAsync(materialTypeModel);
			return materialTypeModel.Id;
		}
	}
}
